package com.example.ymm.modules1.service.impl.hl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.hl.dkn.DknTrialItem;
import com.example.ymm.modules1.entity.hl.dkn.DknTrialLog;
import com.example.ymm.modules1.mapper.hl.DknTrialLogMapper;
import com.example.ymm.modules1.query.hl.DknTrialLogQueryCriteria;
import com.example.ymm.modules1.service.hl.DknTrialItemService;
import com.example.ymm.modules1.service.hl.DknTrialLogService;
import com.example.ymm.util.QueryHelpPlus;
import com.example.ymm.util.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
//@Transactional
@Slf4j
@DS("slave2")
public class DknTrialLogServiceImpl extends BaseServiceImpl<DknTrialLogMapper, DknTrialLog> implements DknTrialLogService {

    @Autowired
    DknTrialItemService dknTrialItemService;

    @Override
    public TableList<DknTrialLog> list(DknTrialLogQueryCriteria criteria, YmPageDto pageDto) {
        IPage<DknTrialLog> page = new Page<DknTrialLog>(pageDto.getPage(),pageDto.getPageSize());
        QueryWrapper predicate = QueryHelpPlus.getPredicate(DknTrialLog.class, criteria);
        predicate.orderByDesc("create_time");
        baseMapper.selectPage(page, predicate);
        TableList<DknTrialLog> tableList=new TableList(page);
        return tableList;
    }

    public List<DknTrialLog> queryAll(DknTrialLogQueryCriteria criteria) {
        QueryWrapper predicate = QueryHelpPlus.getPredicate(DknTrialLog.class, criteria);
        predicate.orderByDesc("create_time");
        List<DknTrialLog> result =baseMapper.selectList(predicate);
        return result;
    }

    @Override
    public void removeByTrialId(String trialId) {
        DknTrialLogQueryCriteria criteria=new DknTrialLogQueryCriteria();
        criteria.setTrialId(trialId);
        List<DknTrialLog> dknTrialLogs = queryAll(criteria);
        if(UtilValidate.isNotEmpty(dknTrialLogs)){
            DknTrialLog trialLog=dknTrialLogs.get(0);
            removeById(trialLog.getId());
            LambdaQueryWrapper<DknTrialItem> lambdaQueryWrapper=new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(DknTrialItem::getPId,trialLog.getId());
            dknTrialItemService.remove(lambdaQueryWrapper);
        }
    }

    @Override
    public void removeByApproveNo(String approveNo) {
        DknTrialLogQueryCriteria criteria=new DknTrialLogQueryCriteria();
        criteria.setApproveNo(approveNo);
        List<DknTrialLog> dknTrialLogs = queryAll(criteria);
        if(UtilValidate.isNotEmpty(dknTrialLogs)){
            DknTrialLog trialLog=dknTrialLogs.get(0);
            removeById(trialLog.getId());
            LambdaQueryWrapper<DknTrialItem> lambdaQueryWrapper=new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(DknTrialItem::getPId,trialLog.getId());
            dknTrialItemService.remove(lambdaQueryWrapper);
        }
    }
}
